﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using NetCoreCMS.IServices;
using NetCoreCMS.Models;

namespace NetCoreCMS.Web.Areas.WebApi.Controllers
{
    public class Cart_Goods_AddController : UserController
    {
        public IArticle_goodsService _article_goods { get; set; }
        public IApp_cart_keysService _app_cart_keys { get; set; }
        public IArticleService _article { get; set; }
        public IUser_groupsService _user_groups { get; set; }
        // GET: api/Cart_Goods_Add
        [Authorize]
        [HttpGet]
        public IActionResult Get(int article_id,int goods_id,int quantity)
        {
            int shop_id = 0;
            var groupModel = GetUserInfo();
            if(groupModel == null)
            {
                return Ok(new { status=0,msg="请先登录！"});
            }
            if (article_id <= 0)
            {
                return Ok(new { status = 0, msg = "您提交的商品参数有误！" });
            }
            //检查商品是否正确
            if (goods_id > 0 && !_article_goods.Exists(article_id, goods_id))
            {
                return Ok(new { status = 0, msg = "您提交的商品参数有误！" });
            }
            //查找会员组
            int group_id = groupModel.group_id;
            //统计购物车
            ShopCart.AppCartHelper shopcart = new ShopCart.AppCartHelper(_app_cart_keys,_article,_user_groups,_article_goods);
            shopcart.Add(article_id, goods_id, quantity, shop_id,groupModel.id);
            cart_total cartModel = shopcart.GetTotal(group_id, groupModel.id);
            return Ok(new { status = 1, msg = "商品已成功添加到购物车！", quantity = cartModel.total_quantity, amount = cartModel.real_amount });
        }
    }
}
